/*
 * Package il.ac.biu.cs.grossmm.test.test3
 * File SpyPublished.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.tests.cpptest;

import static il.ac.biu.cs.grossmm.api.data.NodeTypeByInterface.*;
import static il.ac.biu.cs.grossmm.api.keys.KeyFactory.*;
import static il.ac.biu.cs.grossmm.api.keys.PatternFactory.*;
import static il.ac.biu.cs.grossmm.api.presence.BaseVocabulary.*;
import static il.ac.biu.cs.grossmm.api.presence.ResourceType.*;
import il.ac.biu.cs.grossmm.api.data.Root;
import il.ac.biu.cs.grossmm.api.flow.DataProcessorBase;
import il.ac.biu.cs.grossmm.api.flow.NodeEventInspector;
import il.ac.biu.cs.grossmm.api.flow.NodeInspector;
import il.ac.biu.cs.grossmm.api.flow.Subscriber;
import il.ac.biu.cs.grossmm.api.flow.SubscriptionPoint;
import il.ac.biu.cs.grossmm.api.flow.Status;
import il.ac.biu.cs.grossmm.api.keys.Key;

import java.util.logging.Logger;

public class ProcessorA extends DataProcessorBase implements Subscriber<FooPresence>{
	
	static Logger logger = Logger.getLogger("SpyPublished");
	private SubscriptionPoint<FooPresence> sp;

	public ProcessorA()
	{
		super("Processor A");
		logger.info("Created");
	}

	@Override
	public synchronized void run()
	{
		logger.info("Started");
		
		
		sp = adm.createSubscriptionPoint(this, 
			pattern(String.class, optional(RESOURCE_TYPE, PRESENCE)),
			nodeType(FooPresence.class)
			);
		
		started();
		
		Key key = 
			key("Alice", 
				entry(RESOURCE_TYPE, PRESENCE)
			);
		
		boolean b = sp.subscribe(key, null) == Status.UNMATCHED;
		
		logger.info("Subscribed: "+b);
		
		if( b ) {
			Root<FooPresence> fooRoot = sp.getRoot(key);
			
			logger.info("Root key is " + fooRoot.getKey());
			
			NodeInspector ins = sp.readLock(fooRoot);
			logger.info(ins.toString(fooRoot));
			sp.readUnlock(fooRoot);
		}
		
		waitForStop();
			
		stopped();
		serviceManager.terminated(null);
		
		logger.info("Stopped");
	}

	public void activated(Key key, Root<FooPresence> node)
	{
		NodeInspector ins =  sp.readLock(node);
		logger.info("Activated: " + key+ ins.toString(node));
		sp.readUnlock(node);
	}

	public void deactivated(Key key, Status status)
	{
		logger.info("Deactivated: " + key+status);
	}

	public void notify(Root<FooPresence> node, NodeEventInspector inspector)
	{
		logger.info("Notified: " + inspector.toString(node));
	}

}
